<?php
use yii\helpers\Url;
?>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>铝邦CRM客户管理系统</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/common.css"/>
    <link rel="stylesheet" href="css/public.css"/>
    <link rel="stylesheet" href="css/setting.css"/>
    <script src="js/jquery-1.12.3.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script src="layui/layui.all.js"></script>
</head>
<body class="body-set">
    <div class="main-nav">
        <h3>角色列表</h3>
        <div class="copy-use col-sm-1" data-toggle="modal" data-target="#addUser">复制角色</div>
        <div class="add-use col-sm-1" data-toggle="modal" data-target="#addUser">+新增角色</div>
    </div>
    <div class="set-bar-msg">
        <div class="bar-msg-title">
            <div class="col-sm-2">角色名称</div>
            <div class="col-sm-6">账户个数</div>
            <div class="col-sm-2">备注</div>
            <div class="col-sm-2">操作</div>
        </div>
        <ul class="level-0">
            <?php foreach($rolesList as $value){
            if($value['canDelete']==0){
                ?>
            <li class="level-1">
                <div class="col-sm-2"><?=$value['roleName']?></div>
                <div class="col-sm-6"><a href=""><?=$value['roleCount']?></a></div>
                <div class="col-sm-2">拥有全部权限，不可修改</div>
                <div class="col-sm-2"></div>
            </li>
            <?php
            }else{
            ?>
                <li class="level-1">
                    <div class="col-sm-2"><?=$value['roleName']?></div>
                    <div class="col-sm-6"><a href=""><?=$value['roleCount']?></a></div>
                    <div class="col-sm-2">-------</div>
                    <div class="col-sm-2">
                        <span><img src="images/set-bj.png" info='<?php echo json_encode($value); ?>' onclick="toEditRole(this)"/></span>
                        <span><img class="del-use" roleid="<?=$value['id']?>" src="images/set-del.png"/></span>
                    </div>
                </li>
            <?php
            }
            }
            ?>

        </ul>
    </div>
    <div class="pages">
        <div class="pages-box">
            <a href="" class="count startpage">上一页</a>
            <a href="" class="count">1</a>
            <a href="" class="count on">2</a>
            <a href="" class="count">3</a>
            <span class="page-span">...</span>
            <a href="" class="count">5</a>
            <a href="" class="count endpage">下一页</a>
            <a href="" class="count endpages">末页</a>
            <span>共<span>5</span>条</span>
        </div>
    </div>
    <!-- 模态框 新增部门-->
    <div class="modal fade" id="addUser" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title add-title">新增角色（编辑角色、复制角色）</h4>
                </div>
                <form id="grantForm" action="" method="post">
                <div class="modal-body">
                    <div class="modal-copy">
                        <label class="modal-label">复制的角色：</label>
                        <select name="" id="" class="modal-select">
                            <option value="0">--请选择--</option>
                            <option value="1">主管</option>
                            <option value="2">销售</option>
                        </select>
                    </div>

                    <label class="modal-label">（新）角色名称：</label>
                    <input type="text" placeholder="请输入角色名称" id="roleName" name="roleName" class="modal-input"/>
                    <p>(<span>0</span>/8)</p>
                    <div class="clear"></div>
                    <label class="modal-label">备注：</label>
                    <input class="modal-input-lg" id="remarks" name="remarks" type="text"/>
                    <p>(<span>0</span>/20)</p>
                    <label class="modal-label-left">操作权限</label>
                    <input type="hidden" id="roleId" name="roleId" value="">
                    <div class="modal-pmi">
                        <?php foreach($grantsCatsList as $key=>$value){
                          ?>
                                <div class="pmi-clues">
                                    <div class="pmi-top">
                                        <input type="checkbox" value="线索权限"/><span class="bold"><?=$value['name']?></span>
                                    </div>

                                    <div class="pmi-wap">
                                       <?php foreach($roleGrantsList as $key=>$value2){
                                           if($value['id']==$value2['catId']){
                                        ?>
                                        <input type="checkbox" name="grants[]" value="<?=$value2['uniqueFlag']?>"/><span><?=$value2['name']?></span>
                                        <?php   } } ?>
                                    </div>
                                </div>
                         <?php
                        }?>

                    </div>

                </div>
                </form>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="saveGrant()">保存</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <!--删除-->
    <div class="modal" id="delUser" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title add-title">提示</h4>
                </div>
                <div class="modal-body">
                    <p class="modal-p">确定删除所选角色？该操作成功后，将无法恢复！</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary btn-del" data-dismiss="modal">确定删除</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function () {
            $(".add-use").click(function () {
                $(".modal-copy").css('display','none');
            });
            $(".copy-use").click(function () {
                $(".modal-copy").css('display','block');
            });
            $("#addUser").modal({
                keyboard:false,
                show:false
            });
        })
    </script>
    <script>
        $(".del-use").click(function () {
            var num = $(this).parents("li").find("a").text();
            num = parseInt(num);
            var roleId=$(this).attr('roleid');
            console.log(roleId);
            /* if(num !=0){
             layer.msg("非法操作，该角色下有员工，不能删除！");
             }else{
             $(this).attr({"data-toggle":"modal","data-target":"#delUser"});
             var tt=$(this);
             $(".btn-del").click(function () {
             $(tt).parents("li").remove();
             layer.msg("删除成功");
             })
             }*/
            $(this).attr({"data-toggle": "modal", "data-target": "#delUser"});
            var tt = $(this);
            $(".btn-del").click(function () {

                var url="<?=Url::toRoute(['system/del-role'])?>";
                var roleId=roleId;
                $.post(url,{'roleId':roleId},function(data){
                    var data=JSON.parse(data);
                    if(data.errcode==0){
                        layer.msg(data.errmsg);
                        $(tt).parents("li").remove();
                        window.location.reload();
                    }else{
                        layer.msg(data.errmsg);
                    }
                })


            })

            $(".count").click(function () {

                $(this).siblings().removeClass("on");
                $(this).addClass("on");
                var index = $(this).index();
                if (index == 1) {
                    $(".startpage").hide();
                }
            })
        })
        /**
         * 角色权限
         */
        function saveGrant(){
            var params=$('#grantForm').serialize();
            var url="<?=Url::toRoute(['system/edit-roled'])?>";
            $.post(url,params,function(data){
                var data=JSON.parse(data);
                if(data.errcode==0){
                    layer.msg(data.errmsg);
                    window.location.reload();
                }else{
                    layer.msg(data.errmsg);
                }
            });
        }
        /**
         * 到编辑页面
         */
        function toEditRole(object){

            var params=JSON.parse($(object).attr('info'));
            $('#roleName').val(params.roleName);
            $('#remarks').val(params.remarks);
            $('#roleId').val(params.id);
            $("[name='grants[]']").each(function(){

                var grant=$(this).val();
                if(params['granted'].indexOf(grant)!=-1){
                    //console.log(grant);
                    $(this).attr("checked",'true');
                }


            })
            //console.log(params);
            $("#addUser").modal({
                keyboard:true,
                show:true
            });
        }

    </script>

</body>
</html>